﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmNhaCungCap : Form
    {
        public frmNhaCungCap()
        {
            InitializeComponent();
        }

        private NhaCungCapService nccService = new NhaCungCapService();
        int soluong = 0;

        private void LoadNhaCungCap(DataTable dbtab)
        {
            lv_ncc.Items.Clear();
            lbSoluong.Text = soluong.ToString();
            //DataTable dbtab = nvService.LayTatCaNV();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[6].ToString());
                lv_ncc.Items.Add(liv);
            }
        }

        private void frmNhaCungCap_Load(object sender, EventArgs e)
        {
            DataTable dbtab = nccService.LoadTatCaNhaCungCap();
            LoadNhaCungCap(dbtab);
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmThemNhaCungCap frm = new frmThemNhaCungCap();
            frm.ShowDialog();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            frmThemNhaCungCap frm = new frmThemNhaCungCap();
            frm.ShowDialog();
            DataTable dbtab = nccService.LoadTatCaNhaCungCap();
            LoadNhaCungCap(dbtab);
        }

        private void btnTroVe_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lv_ncc.FocusedItem.Index;
                if (index >= 0)
                {
                    int ma = int.Parse(lv_ncc.Items[index].Text);
                    frmThemNhaCungCap frm = new frmThemNhaCungCap();
                    frm.label1.Text = "Sửa Nhà Cung Cấp";
                    frm.button2.Text = "Sửa";
                    frm.Ncc = nccService.TimNCCID(ma);
                    frm.flag = 1;
                    frm.fix = fix;
                    frm.ShowDialog();
                    DataTable dbtab = nccService.LoadTatCaNhaCungCap();
                    LoadNhaCungCap(dbtab);
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lv_ncc.FocusedItem.Index;
                if (index >= 0)
                {
                    int ma = int.Parse(lv_ncc.Items[index].Text);
                    if (nccService.XoaNCC(ma))
                    {
                        DataTable dbtab = nccService.LoadTatCaNhaCungCap();
                        LoadNhaCungCap(dbtab);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void btn_thanhtoan_Click(object sender, EventArgs e)
        {
            frmHanThanhToan frm = new frmHanThanhToan();
            frm.ShowDialog();
        }

        bool fix=false;
        bool unrepeat = false;
        bool phantom = false;
        bool dirty = false;

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            DataTable dbtab = new DataTable();
            if (fix && dirty)
                dbtab = nccService.LoadNCCDirtyread();
            if(!fix && dirty)
                dbtab = nccService.LoadNCCDirtyreadError();
            if (fix && unrepeat)
                dbtab = nccService.LoadNCCUnrepeat(ref soluong);
            if (!fix && unrepeat)
                dbtab = nccService.LoadNCCUnrepeatError(ref soluong);
            if(fix && phantom)
                dbtab = nccService.LoadNCCPhantom(ref soluong);
            if (!fix && phantom)
                dbtab = nccService.LoadNCCPhantomError(ref soluong);
            if (!fix && !dirty && !unrepeat && !phantom)
                dbtab = nccService.LoadTatCaNhaCungCap();
            LoadNhaCungCap(dbtab);
        }

        private void fixloi_CheckedChanged(object sender, EventArgs e)
        {
            if (fixloi.Checked)
                fix = true;
            else
                fix = false;
        }

        private void cb_unrepeat_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_unrepeat.Checked)
                unrepeat = true;
            else
                unrepeat = false;
        }

        private void cb_phantom_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_phantom.Checked)
                phantom = true;
            else
                phantom = false;
        }

        private void cb_dirty_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_dirty.Checked)
                dirty = true;
            else
                dirty = false;
        }
    }
}
